using Digitalmes.Domain.Entities.Mes.Equipments;

namespace Digitalmes.Application.Mes.Equipments.StandardParts.Commands;

/// <summary>
/// 设备点检标件创建命令。
/// </summary>
/// <param name="StandardParts"></param>
public sealed record CreateStandardPartsCommand(EquipInspectionStandardPartsDto StandardParts) : IRequest<IResult<bool>>;

internal sealed class CreateStandardPartsCommandHandler(IRepository<EquipInspectionStandardParts> repo) : IRequestHandler<CreateStandardPartsCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(CreateStandardPartsCommand request, CancellationToken cancellationToken)
    {
        if (await repo.IsAnyAsync(m => m.StandardPartNo == request.StandardParts.StandardPartNo, cancellationToken))
        {
            return Result.Failure("标准件编号已存在");
        }

        var standardParts = request.StandardParts.Adapt<EquipInspectionStandardParts>();
        var ok = await repo.InsertAsync(standardParts, cancellationToken);
        return Result.From(ok);
    }
}
